<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="流动人口编号" prop="populationCode">
        <el-input
          v-model="queryParams.populationCode"
          placeholder="请输入流动人口编号"
          style="width: 240px"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="populationName">
        <el-input
          v-model="queryParams.populationName"
          placeholder="请输入姓名"
          style="width: 240px"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['population:population:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['population:population:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['population:population:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['population:population:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="populationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键" align="center" prop="populationId" />-->
      <el-table-column label="序号" align="center" width="50" type="index" />
      <el-table-column label="流动人口编号" align="center" width="250" prop="populationCode" >
        <template #default="scope">
          <el-link type="primary" @click="getPopulationDetail(scope.row)" v-hasPermi="['population:population:list']">
            {{ scope.row.populationCode }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="populationName" />
      <el-table-column label="年龄" align="center" prop="populationAge" />
      <el-table-column label="照片" align="center" prop="populationImage" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.populationImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="现居住地" align="center" prop="populationAddress" />
      <el-table-column label="手机号" align="center" prop="populationPhone" />
      <el-table-column label="操作" align="center"
                       width="200px"
                       fixed="right" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['population:population:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['population:population:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <template>
      <!-- 添加或修改流动人口信息对话框 -->
      <el-dialog :title="title" v-model="open" width="800px" append-to-body>
        <el-form ref="populationRef" :model="form" :rules="rules" label-width="110px">
          <!-- 姓名 -->
          <el-form-item label="姓名" prop="populationName">
            <el-input v-model="form.populationName" placeholder="请输入姓名" />
          </el-form-item>

          <!-- 年龄 -->
          <el-form-item label="年龄" prop="populationAge">
            <el-input v-model="form.populationAge" placeholder="请输入年龄" />
          </el-form-item>

          <!-- 照片 -->
          <el-form-item label="照片" prop="populationImage">
            <image-upload v-model="form.populationImage" />
          </el-form-item>

          <!-- 身份证 -->
          <el-form-item label="身份证" prop="populationIdCard">
            <el-input v-model="form.populationIdCard" placeholder="请输入身份证" />
          </el-form-item>

          <!-- 现居住地 -->
          <el-form-item label="现居住地" prop="populationAddress">
            <el-input v-model="form.populationAddress" placeholder="请输入现居住地" />
          </el-form-item>

          <!-- 身份证明附件 -->
          <el-form-item label="身份证明附件" prop="populationIdCardFile">
            <!-- OCR 识别上传按钮 -->
            <file-upload
                :uploadUrl="customUploadUrl"
                :onSuccess="handleUploadSuccess"
                v-model="form.populationIdCardFile"
            />
          </el-form-item>

          <!-- 手机号 -->
          <el-form-item label="手机号" prop="populationPhone">
            <el-input v-model="form.populationPhone" placeholder="请输入手机号" />
          </el-form-item>

          <!-- 居住时间 -->
          <el-form-item label="居住时间" prop="populationTime">
            <el-input v-model="form.populationTime" placeholder="请输入居住时间" />
          </el-form-item>

          <!-- 户口本编号 -->
          <el-form-item label="户口本编号" prop="populationHukouId">
            <el-select v-model="form.populationHukouId" placeholder="请选择所属户口编号" clearable>
              <el-option
                  v-for="item in hukouList"
                  :key="item.id"
                  :label="item.hukouCode"
                  :value="item.hukouId"
              />
            </el-select>
          </el-form-item>

          <!-- 备注 -->
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
          </el-form-item>
        </el-form>

        <!-- 对话框底部按钮 -->
        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </template>
      </el-dialog>
    </template>

    <!-- 详情 -->
    <el-dialog title="流动人口信息详情" v-model="populationDetailOpen" width="800px" append-to-body>
      <el-descriptions border :column="1">
        <el-descriptions-item label="流动人口编号">{{form.populationCode}}</el-descriptions-item>
        <el-descriptions-item label="姓名">{{ form.populationName }}</el-descriptions-item>
        <el-descriptions-item label="年龄">{{ form.populationAge }}</el-descriptions-item>
        <el-descriptions-item label="照片"><image-preview :src="form.populationImage" :width="50" :height="50"/></el-descriptions-item>
        <el-descriptions-item label="身份证">{{ form.populationIdCard }}</el-descriptions-item>
        <el-descriptions-item label="现居住地">{{ form.populationAddress }}</el-descriptions-item>
        <el-descriptions-item label="身份证明附件">
          <el-button type="primary" :disabled="!(form.populationIdCardFile)" @click="download.resource(form.populationIdCardFile)">
            下载
          </el-button>
        </el-descriptions-item>
        <el-descriptions-item label="手机号">{{ form.populationPhone }}</el-descriptions-item>
        <el-descriptions-item label="居住时间">{{ form.populationTime }}</el-descriptions-item>
        <el-descriptions-item label="户口本登记编号">
          {{ (hukouList.find(item => item.hukouId === form.populationHukouId) || {}).hukouCode || '未找到对应的户口登记编号' }}
        </el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ form.createTime }}</el-descriptions-item>
        <el-descriptions-item label="创建人">{{ form.createBy }}</el-descriptions-item>
        <el-descriptions-item label="修改时间">{{ form.updateTime }}</el-descriptions-item>
        <el-descriptions-item label="修改人">{{ form.updateBy }}</el-descriptions-item>
        <el-descriptions-item label="备注" v-if="form.remark && form.remark.trim() !== ''" width="100">
          <el-input v-model="form.remark" type="textarea" :readOnly="true" disabled />
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script setup name="Population">
import { listPopulation, getPopulation, delPopulation, addPopulation, updatePopulation } from "@/api/population/population";
import {listHukou} from "@/api/population/hukou.js";
import {loadAllParams} from "@/api/page.js";
import download from "@/plugins/download.js";

const { proxy } = getCurrentInstance();

const populationList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    populationCode: null,
    populationName: null,
  },
  rules: {
    populationName: [
      { required: true, message: "身份证编号不能为空", trigger: "blur" }
    ],
    populationAge: [
      { required: true, message: "年龄不能为空", trigger: "blur" }
    ],
    populationIdCard: [
      { required: true, message: "身份证编号不能为空", trigger: "blur" }
    ],
    populationAddress: [
      { required: true, message: "现居住地不能为空", trigger: "blur" }
    ],
    populationIdCardFile: [
      { required: true, message: "身份证明附件不能为空", trigger: "blur" }
    ],
    populationHukouId: [
      { required: true, message: "所属户口本编号不能为空", trigger: "change" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

// 自定义区域
const populationDetailOpen = ref(false);
// 户口列表
const hukouList = ref([]);
// 自定义上传地址
const customUploadUrl = ref("/population/population/OCRUpload");
// 上传成功回调函数
const handleUploadSuccess = (data) => {
  // 将返回的 data 数据赋值给 form
  form.value.populationAge = data.populationAge;
  form.value.populationIdCard = data.populationIdCard;
  form.value.populationName = data.populationName;
  form.value.populationPhone = data.populationPhone;
};

/** 查询流动人口信息列表 */
function getList() {
  loading.value = true;
  listPopulation(queryParams.value).then(response => {
    populationList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    populationId: null,
    populationCode: null,
    populationName: null,
    populationAge: null,
    populationImage: null,
    populationIdCard: null,
    populationAddress: null,
    populationIdCardFile: null,
    populationPhone: null,
    populationTime: null,
    populationHukouId: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    remark: null,
    delFlag: null
  };
  proxy.resetForm("populationRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.populationId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加流动人口信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _populationId = row.populationId || ids.value
  getPopulation(_populationId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改流动人口信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["populationRef"].validate(valid => {
    if (valid) {
      if (form.value.populationId != null) {
        updatePopulation(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPopulation(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _populationIds = row.populationId || ids.value;
  proxy.$modal.confirm('是否确认删除流动人口信息编号为"' + _populationIds + '"的数据项？').then(function() {
    return delPopulation(_populationIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('population/population/export', {
    ...queryParams.value
  }, `population_${new Date().getTime()}.xlsx`)
}

getList();

// 自定义区域
// 获取流动人口详情
function getPopulationDetail(row) {
  reset();
  const _id = row.populationId;
  getPopulation(_id).then(response => {
    form.value = response.data;
    populationDetailOpen.value = true;
  })
}
// 获取户口列表
function getHukouList() {
  listHukou(loadAllParams).then(response => {
    hukouList.value = response.rows;
  })
}
getHukouList()
</script>
